import { Common } from '../../utils/common.js';
var common = new Common();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    route_list: '',
    delete_tour_name: "",
    id: "",
    show_action_sheet: false,
    actions: [{
        name: '设为未完成将删除已上传的信息',
        className: 'title',
        disabled: true
      },
      {
        id: 1,
        name: '删除',
        className: 'choose_delete'
      },
      {
        id: 3,
        name: "",
        className: 'choose3'
      },
      {
        id: 2,
        name: '取消',
        className: 'choose_cancel'
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.info) {
      var tour_info = JSON.parse(options.info);
      this.setData({
        tour_info: tour_info,
        show: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var data = { pid: this.data.tour_info.id}
    this.GetAllScheduling(data); //获取所有行程安排
  },

  //所有的行程安排
  GetAllScheduling: function(data) {
    var that = this;
    var datas = {
      data:data,
      type:'post',
      url:'scheduling/list'
    }
    common.DataToData(datas,data => {
      that.setData({
        route_list: data,
      })
    })
  },

  //新赠行程
  detail: function() {
    wx.navigateTo({
      url: '../edit/edit?pid=' + this.data.tour_info.id,
    })
  },

  //编辑行程
  edit: function(e) {
    if (this.data.tour_info.user_id !='0'){
      wx.navigateTo({
        url: '../edit/edit?pid=' + this.data.tour_info.id + '&info=' + JSON.stringify(this.data.route_list[e.currentTarget.dataset.id]),
      })
    }
  },

  //删除选择
  delete_tour: function (e) {
    var that = this;
    that.setData({
      show_action_sheet: true,
      'actions[0].name': "你确定删除行程\"" + that.data.route_list[e.target.dataset.id].name + "\"?",
      delete_id: that.data.route_list[e.target.dataset.id].id,
      id: e.target.dataset.id
    })
  },
  onSelect(event) {
    var id = event.detail.id;
    var that = this;
    if (id == 1) {
      var route_list = that.data.route_list;
      route_list.splice(that.data.id, 1);
      that.setData({
        route_list: route_list,
        show_action_sheet: false,
      });
      var data = { id: that.data.delete_id };
      var datas = {
        data:data,
        type:'post',
        url:'scheduling/del_detail'
      }
      common.DataToCallBack(datas, data => {
        if (data.code == 201) {
          //提示成功
          wx.showToast({
            title: '删除成功',
            icon: 'success', // 'none'为无图标
            duration: 1000, //多少毫秒后消失      
          });
        }
      })
    } else if (id == 2) {
      that.setData({
        show_action_sheet: false,
      })
    }
  },
  onClose() {
    this.setData({
      show: false
    });
  },
})